package com.ybu.oodd.utils.aop;

import com.ybu.oodd.pojo.Result;
import com.ybu.oodd.utils.login.ContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class Admin {
    @Around("execution(public * com.ybu.oodd.controller.AdminController.*(..))")
    public Object aroundAdminController(ProceedingJoinPoint joinPoint) throws Throwable {
        // 判断是否是admin
        if (1 != (Integer) ContextHolder.get("userId")){
            return Result.error("你不是管理员！");
        }

        Object result = null;
        try {
            result = joinPoint.proceed();
        } catch (Exception e) {
            System.out.println("执行异常: " + e.getMessage());
            throw e;
        }
        return result;
    }
}